<?php

class TreeController extends Zend_Controller_Action 
{
    /**
     * Fetch information to render user tree of categories
     * - Ajax request
     *  
     * @return void
     */
    public function inittreeAction() 
    {
        // Disable layout
        $this->_helper->layout->disableLayout();
        
        $factory = Model_Factory::getInstance();
        $service = $factory->getCategoryService();
        
        $userID = Zend_Auth::getInstance()->getIdentity()->oid;
        
        $categories = $service->getUserCategories($userID);
        
        $this->view->categories = $categories;        
    }

    /**
     * Fetch information to render user tree of interesting categories
     * - Ajax request
     *  
     * @return void
     */
    public function inittreesuggestAction()
    {
        // Disable layout
        $this->_helper->layout->disableLayout();
        
        $factory = Model_Factory::getInstance();
        $service = $factory->getCategoryService();
        
        $userID = Zend_Auth::getInstance()->getIdentity()->oid;
        
        $categories = $service->getCategoriesOfInterest($userID);
        
        $this->view->categories = $categories;
    }
    
    /**
     * Fetch informtion to rendere feeds in the user tree
     * - Ajax request
     * 
     * @return void
     */
    public function loadfeedAction()
    {
        // Disable layout
        $this->_helper->layout->disableLayout();
        
        $catID = $this->_getParam('elementID');
        
        if (!empty($catID) && is_numeric($catID) && ($catID > 0)) {
	        $factory = Model_Factory::getInstance();
	        $service = $factory->getFeedService();
	        $newsService = $factory->getNewsService();
	        
	        $userID = Zend_Auth::getInstance()->getIdentity()->oid;
	        
	        $feeds = $service->getUserFeed($userID, $catID);
	        
	        $unreadNewsCount = array();
	        foreach ($feeds as $feed) {
	        	$count = $newsService->getUnreadNewsCount($feed->getOid(), $userID);
	        	$unreadNewsCount += array($feed->getOid() => $count);
	        }
	        
	        $this->view->feeds = $feeds;
	        $this->view->counter = $unreadNewsCount;
        } else {
            $this->_helper->viewRenderer->setNoRender();
            
            $translate = Zend_Registry::get('Zend_Translate');
            $message = $translate->translate('invalidparam');
            $this->_response->appendBody($message);
        }
    }
    
    /**
     * Load feed from preferred categories
     * 
     * @return void
     */
    public function loadfeedsuggestAction()
    {
        // Disable layout
        $this->_helper->layout->disableLayout();
        
        $catID = $this->_getParam('elementID');
        
        if (!empty($catID) && is_numeric($catID) && ($catID > 0)) {
            $factory = Model_Factory::getInstance();
            $service = $factory->getFeedService();
            
            $userID = Zend_Auth::getInstance()->getIdentity()->oid;
            
            $feeds = $service->getSuggestedFeed($catID, $userID);
            
            $this->view->feeds = $feeds;
            
            //return $this->render('loadfeed');
        } else {
            $this->_helper->viewRenderer->setNoRender();
            
            $translate = Zend_Registry::get('Zend_Translate');
            $message = $translate->translate('invalidparam');
            $this->_response->appendBody($message);
        }
    }
}
